define([
	"UtilDir/util",
	"UtilDir/tool",
    "UtilDir/org/selectOrg",
    "UtilDir/searchBlock",
	"Date","DateCN", "css!DateCss",
	"EasyUI","EasyUI-lang",
    ],function(Util, Tool, SelectOrg, SearchBlock){

	var getFxWorkareasController = function () {
		return getServer() + "/fxWorkareas";//查询的是工作区数据
	};

	/**
	 * 页面初始化
	 */
	var fx_echarts;
	var init = function(echarts){
		fx_echarts = echarts;
        initSearchBlock();
        queryBtnBind();
		preEstimateStatistic();
	};

	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
	};
	var queryBtnBind = function(echarts){
        $("#btnSearch").click(function () {
			preEstimateStatistic();
        });
        $("#btnReset").click(function () {
            $("#yearStart").val("");
            $("#yearEnd").val("");
		});
        $("#fx-Preestimate-Statistic-div input[type='radio']").click(function () {
        	console.log($(this).val());
        	if ($(this).val()) {
				changeDataType($(this).val());
			}
		});
	};
	var preEstimateStatistic = function() {
		//console.log("预估统计分析");
		var yearStart = $("#yearStart").val() || "", yearEnd = $("#yearEnd").val() || "";
		$.ajax({
			url: getFxWorkareasController() + "/getFxWorkareaStatisticalAnalysis?yearStart=" + yearStart + "&yearEnd=" + yearEnd,
			type: "get",
			success: function (data) {
				console.log(data);
				var year_str = ""; year2WSNum = {}; year2Area = {}; year2population = {};
					for (let i = 0; i < data.length; i++) {
					year_str = data[i].year;
					!year2WSNum[year_str] ? (year2WSNum[year_str] = 1) : (year2WSNum[year_str] += 1);
					!year2Area[year_str] ? (year2Area[year_str] = data[i].estimateArea) : (year2Area[year_str] += data[i].estimateArea);
					!year2population[year_str] ? (year2population[year_str] = data[i].population) : (year2population[year_str] += data[i].population);
				}
				changeDataType("1");
			}
		});
    };
	var changeDataType = function (dataType) {
		var _years = [], _year_values = [];
		var dom = $("#fx-Preestimate-Statistic").get(0);
		var myChart = fx_echarts.init(dom);
		if (dataType == "1") {
			for (const _year in year2WSNum) {
				_years.push(_year);
				_year_values.push(year2WSNum[_year]);
			}
			fx_echarts_option.legend = {data: ['工作区个数']};
			fx_echarts_option.series[0].name = '工作区个数';
		} else if (dataType == "2") {
			for (const _year in year2Area) {
				_years.push(_year);
				_year_values.push(year2Area[_year]);
			}
			fx_echarts_option.legend = {data: ['工作区面积']};
			fx_echarts_option.series[0].name = '工作区面积';
		} else if (dataType == "3") {
			for (const _year in year2population) {
				_years.push(_year);
				_year_values.push(year2population[_year]);
			}
			fx_echarts_option.legend = {data: ['工作区人口']};
			fx_echarts_option.series[0].name = '工作区人口';
		}
		fx_echarts_option.xAxis[0].data = _years;
		fx_echarts_option.series[0].data = _year_values;
		if (fx_echarts_option && typeof fx_echarts_option === "object") {
			myChart.setOption(fx_echarts_option, true);
		}
	};

	var fx_echarts_option = {
		color: ['#3398DB'],
		legend: {
			data: ['工作区个数']
		},
		tooltip: {
			trigger: 'axis',
			axisPointer: {            // 坐标轴指示器，坐标轴触发有效
				type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
			}
		},
		grid: {
			left: '3%',
			right: '4%',
			bottom: '3%',
			containLabel: true
		},
		xAxis: [
			{
				type: 'category',
				data: [],
				axisTick: {
					alignWithLabel: true
				}
			}
		],
		yAxis: [
			{
				type: 'value'
			}
		],
		series: [
			{
				name: '工作区个数',
				type: 'bar',
				barWidth: '60%',
				data: []
			}
		]
	}, year2WSNum = {}, year2Area = {}, year2population = {};

	return {
		init:init
	};
});
